package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j//使用lombok的@Slf4j，帮我们创建Logger对象，效果与下方获取日志对象一样
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
//@MapperScan
//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class BasedemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BasedemoApplication.class, args);
    }

    /**
     * 配置内部类
     */

    @Controller
    @Configuration
    class Config {

        /**
         * 获取日志对象，构造函数传入当前类，查找日志方便定位
         */
        private final Logger logger = LoggerFactory.getLogger(this.getClass());

        @Value("${user.home}")
        private String userName;

        @Value("${server.http.port}")
        private String httpport;

        @Value("${server.ssl.enabled}")
        private boolean sslEnabled;

        /**
         * 端口
         */
        @Value("${server.port}")
        private String port;

        /**
         * 启动成功
         */
        @Bean
        public ApplicationRunner applicationRunner() {
            return applicationArguments -> {
                try {
                    InetAddress ia = InetAddress.getLocalHost();
                    //判断是否开启https，获取本机内网IP
                    if (sslEnabled){
                        log.info("启动成功：" + "https://" + ia.getHostAddress() + ":" + port + "/");
                        logger.info("${user.home} ：" + userName);
                    }else {
                        log.info("启动成功：" + "http://" + ia.getHostAddress() + ":" + httpport + "/");
                        logger.info("${user.home} ：" + userName);
                    }

                } catch (UnknownHostException ex) {
                    ex.printStackTrace();
                }
            };
        }

    }
    }
